package com.yjd.bi.statistic.control;

import com.yjd.bi.comm.utils.BiDateUtil;
import com.yjd.bi.depository.model.BasicDataDepoModel;
import com.yjd.bi.depository.service.IBasicDataDepoService;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.user.service.IBasicDataPcService;
import com.yjd.comm.annotation.DateString;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangchangyan on 2017/5/16.
 */
@Controller
@Scope("prototype")
@RequestMapping("/depo/stist")
public class DepoDataStatisticControl extends BaseControl {

    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    IBasicDataDepoService basicDataDepoService;

    @RequestMapping(value = "/index")
    public String index() {
        request.setAttribute("start", "2017-05-01");
        request.setAttribute("end", DateUtil.timestampToDateString(Calendar.getInstance().getTimeInMillis()/1000L - 86400, "yyyy-MM-dd"));
        return "basic.depo.index";
    }

    @RequestMapping(value = "/summary")
    public void summary(@DateString String start, @DateString String end) throws Exception {

        Long beginDate = Long.parseLong(start);
        Long endDate = Long.parseLong(end);
        if(beginDate > endDate){
            Long tmp = endDate;
            endDate = beginDate;
            beginDate = tmp;
        }

        BasicDataDepoModel basicDataDepoModel = basicDataDepoService.countByDatePeriod(beginDate, endDate);
        if (null == basicDataDepoModel) {
            basicDataDepoModel = new BasicDataDepoModel();
        }

        Map<String, Object> resultMap = new HashMap<String, Object>();

        // 存管白名单人数
        resultMap.put("depo_whitelist_account", basicDataDepoModel.getDepo_whitelist_account());

        // 开通存管人数
        resultMap.put("depo_account", basicDataDepoModel.getDepo_account());

        // 开通存管后绑卡人数
        resultMap.put("depo_bind_card", basicDataDepoModel.getDepo_bind_card());

        // 开通存管后首次充值人数
        resultMap.put("first_recharge", basicDataDepoModel.getFirst_recharge());

        // 开通存管后首次投资人数
        resultMap.put("first_invest", basicDataDepoModel.getFirst_invest());

        // 开通存管后首次提现人数
        resultMap.put("first_cashOut", basicDataDepoModel.getFirst_cashout());

        // 开通存管后充值金额
        resultMap.put("rech_price", basicDataDepoModel.getRech_price());

        // 开通存管后投资金额
        resultMap.put("invest_price", basicDataDepoModel.getInvest_price());

        // 开通存管后首次提现人数
        resultMap.put("cashOut_price", basicDataDepoModel.getCashout_price());

        super.successData(resultMap);
    }

    @RequestMapping(value = "/year")
    public void year() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int count_70 = basicDataDepoService.countDepoBankByYear(1970, 1979);//统计70后
        resultMap.put("depo_year_count_70", count_70);

        int count_80 = basicDataDepoService.countDepoBankByYear(1980, 1989);//统计80后
        resultMap.put("depo_year_count_80", count_80);

        int count_90 = basicDataDepoService.countDepoBankByYear(1990, 1999);//统计90后
        resultMap.put("depo_year_count_90", count_90);

        int  count_all = basicDataDepoService.countDepoBankByYear(null, null);//统计90后
        resultMap.put("depo_year_count_other", count_all-count_90-count_80-count_70);

        super.successData(resultMap);
    }

    /**
     * 获取平台流失用户数
     * @throws Exception
     */
    @RequestMapping(value = "/loss")
    public String lossUser() throws Exception {
        request.setAttribute("start", "2017-12-01");
        request.setAttribute("end", BiDateUtil.initBiDate());
        return "loss";
    }

    @RequestMapping(value = "/investing")
    public String investingUser() throws Exception {
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("end", BiDateUtil.initBiDate());
        return "investing";
    }

    /**
     * 获取平台在投用户数
     * @throws Exception
     */
    @RequestMapping(value = "/getInvestingNums")
    public void getInvestingNums(@RequestParam Integer timeType, @DateString String timeFrom) throws Exception {
        EChartsModel echartsModel = basicDataPcService.getRetentionDates(
                TimeType.getType(timeType), Long.valueOf(timeFrom), false);
        ServletUtil.outputJsonMessage(request, response, echartsModel);
    }

    /**
     * 获取平台流失用户数
     * @throws Exception
     */
    @RequestMapping(value = "/getLossNums")
    public void getLossNums(@RequestParam Integer timeType, @DateString String timeFrom) throws Exception {
        EChartsModel echartsModel = basicDataPcService.getRetentionDates(
                TimeType.getType(timeType), Long.valueOf(timeFrom), true);
        ServletUtil.outputJsonMessage(request, response, echartsModel);
    }

    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IBasicDataPcService basicDataPcService;
}
